package org.htmlparser.nodes;

import java.util.Hashtable;
import java.util.Locale;
import java.util.Vector;
import org.htmlparser.Attribute;
import org.htmlparser.Tag;
import org.htmlparser.lexer.Cursor;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.scanners.Scanner;
import org.htmlparser.scanners.TagScanner;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.NodeVisitor;

public class TagNode extends AbstractNode
  implements Tag
{
  private static final String[] NONE = new String[0];
  protected static Hashtable breakTags;
  protected static final Scanner mDefaultScanner = new TagScanner();
  protected Vector mAttributes;
  private Scanner mScanner;

  static
  {
    breakTags = new Hashtable(30);
    breakTags.put("BLOCKQUOTE", Boolean.TRUE);
    breakTags.put("BODY", Boolean.TRUE);
    breakTags.put("BR", Boolean.TRUE);
    breakTags.put("CENTER", Boolean.TRUE);
    breakTags.put("DD", Boolean.TRUE);
    breakTags.put("DIR", Boolean.TRUE);
    breakTags.put("DIV", Boolean.TRUE);
    breakTags.put("DL", Boolean.TRUE);
    breakTags.put("DT", Boolean.TRUE);
    breakTags.put("FORM", Boolean.TRUE);
    breakTags.put("H1", Boolean.TRUE);
    breakTags.put("H2", Boolean.TRUE);
    breakTags.put("H3", Boolean.TRUE);
    breakTags.put("H4", Boolean.TRUE);
    breakTags.put("H5", Boolean.TRUE);
    breakTags.put("H6", Boolean.TRUE);
    breakTags.put("HEAD", Boolean.TRUE);
    breakTags.put("HR", Boolean.TRUE);
    breakTags.put("HTML", Boolean.TRUE);
    breakTags.put("ISINDEX", Boolean.TRUE);
    breakTags.put("LI", Boolean.TRUE);
    breakTags.put("MENU", Boolean.TRUE);
    breakTags.put("NOFRAMES", Boolean.TRUE);
    breakTags.put("OL", Boolean.TRUE);
    breakTags.put("P", Boolean.TRUE);
    breakTags.put("PRE", Boolean.TRUE);
    breakTags.put("TD", Boolean.TRUE);
    breakTags.put("TH", Boolean.TRUE);
    breakTags.put("TITLE", Boolean.TRUE);
    breakTags.put("UL", Boolean.TRUE);
  }

  public TagNode()
  {
    this(null, -1, -1, new Vector());
  }

  public TagNode(Page paramPage, int paramInt1, int paramInt2, Vector paramVector)
  {
    super(paramPage, paramInt1, paramInt2);
    this.mScanner = mDefaultScanner;
    this.mAttributes = paramVector;
    if ((this.mAttributes == null) || (this.mAttributes.size() == 0))
    {
      String[] arrayOfString = getIds();
      if ((arrayOfString == null) || (arrayOfString.length == 0))
        break label63;
      setTagName(arrayOfString[0]);
    }
    while (true)
    {
      return;
      label63: setTagName("");
    }
  }

  public TagNode(TagNode paramTagNode, TagScanner paramTagScanner)
  {
    this(paramTagNode.getPage(), paramTagNode.getTagBegin(), paramTagNode.getTagEnd(), paramTagNode.getAttributesEx());
    setThisScanner(paramTagScanner);
  }

  public void accept(NodeVisitor paramNodeVisitor)
  {
    if (isEndTag())
      paramNodeVisitor.visitEndTag(this);
    while (true)
    {
      return;
      paramNodeVisitor.visitTag(this);
    }
  }

  public boolean breaksFlow()
  {
    return breakTags.containsKey(getTagName());
  }

  public String getAttribute(String paramString)
  {
    String str = null;
    Attribute localAttribute = getAttributeEx(paramString);
    if (localAttribute != null)
      str = localAttribute.getValue();
    return str;
  }

  public Attribute getAttributeEx(String paramString)
  {
    Object localObject = null;
    Vector localVector = getAttributesEx();
    if (localVector != null)
    {
      int i = localVector.size();
      for (int j = 0; j < i; ++j)
      {
        Attribute localAttribute = (Attribute)localVector.elementAt(j);
        String str = localAttribute.getName();
        if ((str != null) && (paramString.equalsIgnoreCase(str)))
        {
          localObject = localAttribute;
          j = i;
        }
      }
    }
    return localObject;
  }

  public Vector getAttributesEx()
  {
    return this.mAttributes;
  }

  public Tag getEndTag()
  {
    return null;
  }

  public String[] getEndTagEnders()
  {
    return NONE;
  }

  public String[] getEnders()
  {
    return NONE;
  }

  public int getEndingLineNumber()
  {
    return super.getPage().row(super.getEndPosition());
  }

  public String[] getIds()
  {
    return NONE;
  }

  public String getRawTagName()
  {
    String str = null;
    Vector localVector = getAttributesEx();
    if (localVector.size() != 0)
      str = ((Attribute)localVector.elementAt(0)).getName();
    return str;
  }

  public int getStartingLineNumber()
  {
    return super.getPage().row(super.getStartPosition());
  }

  public int getTagBegin()
  {
    return this.nodeBegin;
  }

  public int getTagEnd()
  {
    return this.nodeEnd;
  }

  public String getTagName()
  {
    String str = getRawTagName();
    if (str != null)
    {
      str = str.toUpperCase(Locale.ENGLISH);
      if (str.startsWith("/"))
        str = str.substring(1);
      if (str.endsWith("/"))
        str = str.substring(0, str.length() - 1);
    }
    return str;
  }

  public String getText()
  {
    String str = super.toHtml();
    return str.substring(1, str.length() - 1);
  }

  public Scanner getThisScanner()
  {
    return this.mScanner;
  }

  public boolean isEmptyXmlTag()
  {
    int i = 0;
    Vector localVector = getAttributesEx();
    int j = localVector.size();
    if (j > 0)
    {
      String str = ((Attribute)localVector.elementAt(j - 1)).getName();
      if (str != null)
      {
        if (str.charAt(str.length() - 1) != '/')
          break label57;
        i = 1;
      }
    }
    while (true)
    {
      return i;
      label57: i = 0;
    }
  }

  public boolean isEndTag()
  {
    int i;
    String str = getRawTagName();
    if (str == null)
      i = 0;
    while (true)
    {
      while (true)
      {
        return i;
        if ((str.length() == 0) || ('/' != str.charAt(0)))
          break;
        i = 1;
      }
      i = 0;
    }
  }

  public void removeAttribute(String paramString)
  {
    Attribute localAttribute = getAttributeEx(paramString);
    if (localAttribute != null)
      getAttributesEx().remove(localAttribute);
  }

  public void setAttribute(String paramString1, String paramString2)
  {
    char c1;
    int i = 0;
    int j = 1;
    int k = 1;
    if (paramString2 != null)
    {
      int i1 = 0;
      if (i1 < paramString2.length())
      {
        char c3 = paramString2.charAt(i1);
        if (Character.isWhitespace(c3))
          i = 1;
        while (true)
        {
          do
            while (true)
            {
              while (true)
                ++i1;
              if ('\'' != c3)
                break;
              j = 0;
            }
          while ('"' != c3);
          k = 0;
        }
      }
    }
    if (i != 0)
      if (k != 0)
      {
        c1 = '"';
        Attribute localAttribute = getAttributeEx(paramString1);
        if (localAttribute == null)
          break label217;
        localAttribute.setValue(paramString2);
        if (c1 != 0)
          localAttribute.setQuote(c1);
      }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            return;
            if (j == 0)
              break;
            c1 = '\'';
          }
          c1 = '"';
          StringBuffer localStringBuffer = new StringBuffer(5 * paramString2.length());
          int l = 0;
          if (l < paramString2.length())
          {
            char c2 = paramString2.charAt(l);
            if (c1 == c2)
              localStringBuffer.append("&quot;");
            while (true)
            {
              while (true)
                ++l;
              localStringBuffer.append(c2);
            }
          }
          paramString2 = localStringBuffer.toString();
        }
        c1 = ';
      }
      label217: setAttribute(paramString1, paramString2, c1);
    }
  }

  public void setAttribute(String paramString1, String paramString2, char paramChar)
  {
    setAttribute(new Attribute(paramString1, paramString2, paramChar));
  }

  public void setAttribute(Attribute paramAttribute)
  {
    int i = 0;
    Vector localVector = getAttributesEx();
    int j = localVector.size();
    if (j > 0)
    {
      String str1 = paramAttribute.getName();
      for (int k = 1; k < localVector.size(); ++k)
      {
        String str2 = ((Attribute)localVector.elementAt(k)).getName();
        if ((str2 != null) && (str2.equalsIgnoreCase(str1)))
        {
          localVector.setElementAt(paramAttribute, k);
          i = 1;
        }
      }
    }
    if (i == 0)
    {
      if ((j != 0) && (!(((Attribute)localVector.elementAt(j - 1)).isWhitespace())))
        localVector.addElement(new Attribute(" "));
      localVector.addElement(paramAttribute);
    }
  }

  public void setAttributeEx(Attribute paramAttribute)
  {
    setAttribute(paramAttribute);
  }

  public void setAttributesEx(Vector paramVector)
  {
    this.mAttributes = paramVector;
  }

  public void setEmptyXmlTag(boolean paramBoolean)
  {
    String str;
    int j;
    Vector localVector = getAttributesEx();
    int i = localVector.size();
    if (i > 0)
    {
      Attribute localAttribute1 = (Attribute)localVector.elementAt(i - 1);
      str = localAttribute1.getName();
      if (str != null)
      {
        j = str.length();
        if (localAttribute1.getValue() == null)
          if (str.charAt(j - 1) == '/')
            if (!(paramBoolean))
            {
              if (1 != j)
                break label85;
              localVector.removeElementAt(i - 1);
            }
      }
    }
    while (true)
    {
      do
        while (true)
        {
          do
            while (true)
            {
              do
                while (true)
                {
                  do
                    while (true)
                    {
                      return;
                      label85: Attribute localAttribute2 = new Attribute(str.substring(0, j - 1), null);
                      localVector.removeElementAt(i - 1);
                      localVector.addElement(localAttribute2);
                    }
                  while (!(paramBoolean));
                  localVector.addElement(new Attribute(" "));
                  localVector.addElement(new Attribute("/", null));
                }
              while (!(paramBoolean));
              localVector.addElement(new Attribute(" "));
              localVector.addElement(new Attribute("/", null));
            }
          while (!(paramBoolean));
          localVector.addElement(new Attribute("/", null));
        }
      while (!(paramBoolean));
      localVector.addElement(new Attribute("/", null));
    }
  }

  public void setEndTag(Tag paramTag)
  {
  }

  public void setTagBegin(int paramInt)
  {
    this.nodeBegin = paramInt;
  }

  public void setTagEnd(int paramInt)
  {
    this.nodeEnd = paramInt;
  }

  public void setTagName(String paramString)
  {
    Attribute localAttribute1 = new Attribute(paramString, null, ');
    Vector localVector = getAttributesEx();
    if (localVector == null)
    {
      localVector = new Vector();
      setAttributesEx(localVector);
    }
    if (localVector.size() == 0)
      localVector.addElement(localAttribute1);
    while (true)
    {
      while (true)
      {
        return;
        Attribute localAttribute2 = (Attribute)localVector.elementAt(0);
        if ((localAttribute2.getValue() != null) || (localAttribute2.getQuote() != 0))
          break;
        localVector.setElementAt(localAttribute1, 0);
      }
      localVector.insertElementAt(localAttribute1, 0);
    }
  }

  public void setText(String paramString)
  {
    Lexer localLexer = new Lexer(paramString);
    try
    {
      TagNode localTagNode = (TagNode)localLexer.nextNode();
      this.mPage = localTagNode.getPage();
      this.nodeBegin = localTagNode.getStartPosition();
      this.nodeEnd = localTagNode.getEndPosition();
      this.mAttributes = localTagNode.getAttributesEx();
      return;
    }
    catch (ParserException localParserException)
    {
      throw new IllegalArgumentException(localParserException.getMessage());
    }
  }

  public void setThisScanner(Scanner paramScanner)
  {
    this.mScanner = paramScanner;
  }

  public String toHtml(boolean paramBoolean)
  {
    int i = 2;
    Vector localVector = getAttributesEx();
    int j = localVector.size();
    for (int k = 0; k < j; ++k)
      i += ((Attribute)localVector.elementAt(k)).getLength();
    StringBuffer localStringBuffer = new StringBuffer(i);
    localStringBuffer.append("<");
    for (int l = 0; l < j; ++l)
      ((Attribute)localVector.elementAt(l)).toString(localStringBuffer);
    localStringBuffer.append(">");
    return localStringBuffer.toString();
  }

  public String toPlainTextString()
  {
    return "";
  }

  public String toString()
  {
    String str2;
    String str1 = getText();
    StringBuffer localStringBuffer = new StringBuffer(20 + str1.length());
    if (isEndTag())
    {
      str2 = "End";
      Cursor localCursor1 = new Cursor(super.getPage(), super.getStartPosition());
      Cursor localCursor2 = new Cursor(super.getPage(), super.getEndPosition());
      localStringBuffer.append(str2);
      localStringBuffer.append(" (");
      localStringBuffer.append(localCursor1);
      localStringBuffer.append(",");
      localStringBuffer.append(localCursor2);
      localStringBuffer.append("): ");
      if (80 >= localStringBuffer.length() + str1.length())
        break label160;
      localStringBuffer.append(str1.substring(0, 77 - localStringBuffer.length()));
      localStringBuffer.append("...");
    }
    while (true)
    {
      while (true)
      {
        return localStringBuffer.toString();
        str2 = "Tag";
      }
      label160: localStringBuffer.append(str1);
    }
  }
}